<template>
  <MainPage>
    <template v-slot:HeaderLeft>
      <el-form
        :inline="true"
        :model="form"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="70px"
      >
        <el-form-item label="配送日期" prop="sendTime">
          <el-date-picker
            v-model="sendTime"
            type="daterange"
            class="time220"
            :editable="false"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            :clearable="false"
            :picker-options="dateOption"
          />
        </el-form-item>
        <el-form-item label="订单" prop="order_sn">
          <el-input
            v-model.trim="form.order_sn"
            placeholder="订单"
            clearable
            class="wid220"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户" prop="uid">
          <change-user-input
            ref="changeUserInput"
            @sendValue="getUser"
            @clear="clearUid"
          ></change-user-input>
        </el-form-item>
        <div class="more el-form-item" @click="moredata = !moredata">
          <span>更多</span>
          <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
        </div>
        <div class="clearfix"></div>
        <el-collapse-transition>
          <div v-if="moredata">
            <div>
              <el-form-item label="下单日期" prop="createTime">
                <el-date-picker
                  v-model="createTime"
                  type="daterange"
                  class="time220"
                  :editable="false"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                  :clearable="false"
                  :picker-options="dateOption"
                />
              </el-form-item>
              <el-form-item label="收货信息" prop="contact">
                <el-input
                  v-model.trim="form.contact"
                  placeholder="收货人/收货手机"
                  clearable
                  class="wid220"
                ></el-input>
              </el-form-item>
              <el-form-item label="业务员" prop="salesman_id">
                <el-select
                  v-model.trim="form.salesman_id"
                  placeholder="单据类型"
                  class="wid220"
                >
                  <el-option label="所有" value=""></el-option>
                  <el-option
                    v-for="(item, index) in salesman"
                    :key="index"
                    :label="item"
                    :value="index"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="对账状态" prop="account_status">
                <el-select
                  v-model.trim="form.account_status"
                  placeholder="对账状态"
                  class="wid220"
                >
                  <el-option label="所有" value=""></el-option>
                  <el-option
                    v-for="(item, index) in account_status_list"
                    :key="index"
                    :label="item"
                    :value="index + 1"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>
        </el-collapse-transition>
        <el-form-item class="marginTop5">
          <el-button class="reset marginLeft10" type="primary" @click="reset"
            >重置</el-button
          >
          <el-button
            type="primary"
            @click="getArticleList"
            :loading="listLoading"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:HeaderRight>
      <router-link to="/financial/other/user" class="mainor_tea"
        >目标用户</router-link
      >
      <el-button @click="pldeliver"
        >批量打印
        <el-tooltip effect="dark" placement="bottom">
          <div slot="content">
            注释：打印机纸张需要后进纸，连续打印没有预览效果
          </div>
          <svg aria-hidden="true" class="new_navbar_svg" width="14" height="14">
            <use xlink:href="#icon-summary_question" />
          </svg> </el-tooltip
      ></el-button>
    </template>
    <template v-slot:Table>
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        element-loading-text="加载中"
        fit
        border
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column width="60" type="selection" />
        <el-table-column
          label="订单号"
          prop="order_sn"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="用户名称"
          prop="nickname"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="下单日期" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ parseTime(scope.row.created_at, "{y}-{m}-{d}") }}
          </template>
        </el-table-column>
        <el-table-column label="配送日期" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ parseTime(scope.row.send_time, "{y}-{m}-{d}") }}
          </template></el-table-column
        >
        <el-table-column
          label="原下单金额"
          prop="should_money"
          :show-overflow-tooltip="true"
          v-if="show_original_order_data"
        />

        <el-table-column
          label="下单金额"
          prop="nickname"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{
              scope.row.order_empty
                ? scope.row.order_empty.should_money
                : scope.row.should_money
            }}
          </template>
        </el-table-column>

        <el-table-column
          label="原配送金额"
          prop="send_money"
          :show-overflow-tooltip="true"
          v-if="show_original_order_data"
        />
        <el-table-column
          label="配送金额"
          prop="send_money"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{
              scope.row.order_empty
                ? scope.row.order_empty.send_money
                : scope.row.send_money
            }}
          </template></el-table-column
        >
        <el-table-column label="对账状态" prop="" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span :class="'account_status' + scope.row.account_status">{{
              account_status_list[scope.row.account_status]
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="管理操作"
          align="center"
          fixed="right"
          width="240"
        >
          <template slot-scope="scope">
            <router-link :to="'/financial/other/see/' + scope.row.id">
              <el-link type="success">查看</el-link></router-link
            >

            <router-link :to="'/financial/other/account/' + scope.row.id">
              <el-link type="warning">对账</el-link></router-link
            >
            <el-link @click="print(scope.row)">打印</el-link>
            <el-link style="color: #118ab2" @click="handleExport(scope.row.id)"
              >导出</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template v-slot:Page>
      <el-pagination
        :current-page="current.page"
        :page-sizes="[20, 50, 100, 200]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        background
        @size-change="changeLimit"
        @current-change="changePage"
      />
    </template>
    <template v-slot:Dialog>
      <!-- 打印子组件 -->
      <order-print
        ref="printTemplate"
        v-show="false"
        id="printname"
      ></order-print>
    </template>
  </MainPage>
</template>
<script>
import {
  get_empty_index,
  batch_unbind_export
} from "@/api/financial/finaicalOther";
import MainPage from "@/components/MainPage";
import dateOption from "@/options/dateOption";
import { parseTime, exportMethod, getNowMonth } from "@/utils/index";
import OrderPrint from "@/views/order/orderPrint.vue";
import { mapGetters } from "vuex";
export default {
  components: {
    MainPage,
    OrderPrint
  },
  data() {
    return {
      dateOption,
      parseTime,
      exportMethod,
      createTime: [],
      sendTime: [],
      form: {
        send_start_time: "",
        send_end_time: "",
        create_start_time: "",
        create_end_time: "",
        uid: "",
        contact: "",
        order_sn: "",
        salesman_id: "",
        account_status: ""
      },
      moredata: false,
      list: [],
      salesman: {},
      account_status_list: [],
      listLoading: false,
      // 分页
      current: {
        page: 1,
        per_page: 20
      },
      limit: 20,
      totalCount: 1,
      selectArr: []
    };
  },
  computed: {
    ...mapGetters({
      show_original_order_data: "show_original_order_data"
    })
  },
  created() {
    this.get_empty_index();
  },
  methods: {
    //获取用户信息
    getUser(data) {
      let uid = data.info.user_id;
      this.form.uid = uid;
    },
    // 清除用户的uid
    clearUid() {
      this.form.uid = "";
    },
    getArticleList() {
      this.current.page = 1;
      this.get_empty_index();
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.form.uid = "";
      this.$refs["changeUserInput"].name = "";
      this.createTime = [];
      this.sendTime = [];
      this.getArticleList();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.get_empty_index();
    },
    changePage(page) {
      this.current.page = page;
      this.get_empty_index();
    },
    get_empty_index() {
      let data = Object.assign(this.list_form(), this.current);
      this.listLoading = true;
      get_empty_index(data).then(res => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.salesman = res.data.salesman;
          this.account_status_list = res.data.account_status_list;
          this.totalCount = res.data.list.total;
          this.list = res.data.list.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.send_start_time = this.sendTime ? this.sendTime[0] : "";
      form.send_end_time = this.sendTime ? this.sendTime[1] : "";
      form.create_start_time = this.createTime ? this.createTime[0] : "";
      form.create_end_time = this.createTime ? this.createTime[1] : "";
      return form;
    },
    handleExport(id) {
      let data = { id: id };
      batch_unbind_export(data).then(res => {
        this.exportMethod(res, "用户其他对账");
      });
    },
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map(item => {
        this.selectArr.push({ id: item.id, uid: item.uid });
      });
    },
    // 打印
    print(row) {
      let id = row.id;
      let uid = row.uid;
      let continuous = row.continuous;
      this.$refs.printTemplate.$emit("demo", {
        id: id,
        uid: uid,
        type: "other_user_order",
        continuous: continuous
      });
    },
    pldeliver() {
      if (this.selectArr.length != 0) {
        let length = this.selectArr.length;
        this.$confirm(
          `此操作将连续打印【${length}】张配送单,并且打印机需要设置为后进纸,是否继续?`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
          .then(() => {
            this.selectArr.map(e => {
              setTimeout(() => {
                this.$nextTick(() => {
                  let row = {
                    id: e.id,
                    uid: e.uid,
                    continuous: "continuous"
                  };
                  this.print(row);
                });
              }, 500);
            });
          })
          .catch(() => {});
      } else {
        this.$message.error("请勾选订单");
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.account_status0 {
  color: red;
}
.account_status1 {
  color: #03a98e;
}
.new_navbar_svg {
  vertical-align: bottom;
}
</style>
